﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace YJKRevitAxisModule
{
	public class HideOrShowGridTag
	{
		public HideOrShowGridTag()
		{
			this.m_DicGridStatus = new Dictionary<ElementId, List<GridStatusEachView>>();
		}

		public void DoRequest(object parameters = null)
		{
			RequestParam requestParam = (RequestParam)parameters;
			ExternalCommandData commandData = requestParam.m_commandData;
			this.m_HideComment = requestParam.m_Frm.CommentChecked;
			this.m_HideRange = requestParam.m_Frm.RangeChecked;
			this.m_SelMode = requestParam.m_Frm.SelMode;
			List<Element> list = new List<Element>();
			list.AddRange(requestParam.m_lstGrid);
			this.m_Doc = commandData.Application.ActiveUIDocument.Document;
			Transaction transaction = new Transaction(this.m_Doc, "隐藏或者显示轴线标头");
			try
			{
				this.RefreshAllGridStatus();
				List<ElementId> list2 = HideOrShowGridTag.DealMultiKindGridToSimpleKind(list);
				this.DealGridByTagStatus(list2);
				HideOrShowGridTag.ModifyGridStatusByXdata(list2, this.m_Doc, this.m_DicGridStatus);
				transaction.Start();
				foreach (KeyValuePair<ElementId, List<GridStatusEachView>> keyValuePair in this.m_DicGridStatus)
				{
					GridXdataManger.WriteBubbleStatus(this.m_Doc.GetElement(keyValuePair.Key), keyValuePair.Value);
				}
				transaction.Commit();
			}
			catch (Exception)
			{
				if (transaction.IsValidObject && transaction.HasStarted())
				{
					transaction.RollBack();
				}
			}
		}

		private void RefreshAllGridStatus()
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_Doc);
			ElementClassFilter elementClassFilter = new ElementClassFilter(typeof(Grid));
			filteredElementCollector.WherePasses(elementClassFilter).WhereElementIsNotElementType();
			FilteredElementCollector filteredElementCollector2 = new FilteredElementCollector(this.m_Doc);
			ElementClassFilter elementClassFilter2 = new ElementClassFilter(typeof(Autodesk.Revit.DB.View));
			filteredElementCollector2.WherePasses(elementClassFilter2).WhereElementIsNotElementType();
			this.m_lstViewId = new List<ElementId>();
			this.m_lstViewId.Clear();
			foreach (Element element in filteredElementCollector2)
			{
				if (element is Autodesk.Revit.DB.View)
				{
					Autodesk.Revit.DB.View view = element as Autodesk.Revit.DB.View;
                    int viewType = (int)view.ViewType;
					if (viewType == 1 || viewType == 2 || viewType == 3 || viewType == 115 || viewType == 116 || viewType == 117)
					{
						this.m_lstViewId.Add(view.Id);
					}
				}
			}
			foreach (Element element2 in filteredElementCollector)
			{
				foreach (ElementId elementId in this.m_lstViewId)
				{
					Autodesk.Revit.DB.View view2 = this.m_Doc.GetElement(elementId) as Autodesk.Revit.DB.View;
					Grid grid = element2 as Grid;
					if (grid.CanBeVisibleInView(view2))
					{
						GridStatusEachView item = default(GridStatusEachView);
						if (grid.HasBubbleInView((DatumEnds)0, view2) && grid.IsBubbleVisibleInView((DatumEnds)0, view2))
						{
							if (grid.HasBubbleInView((DatumEnds)1, view2) && grid.IsBubbleVisibleInView((DatumEnds)1, view2))
							{
								item.nHasTag = 2;
							}
							else
							{
								item.nHasTag = 0;
							}
						}
						else if (grid.HasBubbleInView((DatumEnds)1, view2) && grid.IsBubbleVisibleInView((DatumEnds)1, view2))
						{
							item.nHasTag = 1;
						}
						else
						{
							item.nHasTag = -1;
						}
						item.ViewID = view2.Id;
						item.nStatus = 0;
						if (this.m_DicGridStatus.ContainsKey(element2.Id))
						{
							this.m_DicGridStatus[element2.Id].Add(item);
						}
						else
						{
							List<GridStatusEachView> list = new List<GridStatusEachView>();
							list.Add(item);
							this.m_DicGridStatus.Add(element2.Id, list);
						}
					}
				}
				List<GridStatusEachView> list2 = new List<GridStatusEachView>();
				if (GridXdataManger.ReadBubbleStatus(element2, ref list2))
				{
					using (List<GridStatusEachView>.Enumerator enumerator3 = list2.GetEnumerator())
					{
						while (enumerator3.MoveNext())
						{
							GridStatusEachView status = enumerator3.Current;
							int num = this.m_DicGridStatus[element2.Id].FindIndex((GridStatusEachView e) => e.ViewID == status.ViewID);
							if (num != -1)
							{
								this.m_DicGridStatus[element2.Id][num] = status;
							}
						}
					}
				}
			}
		}

		public static List<ElementId> DealMultiKindGridToSimpleKind(List<Element> lstMutiKindGrid)
		{
			List<ElementId> list = new List<ElementId>();
			foreach (Element element in lstMutiKindGrid)
			{
				if (element is MultiSegmentGrid)
				{
					list.AddRange((element as MultiSegmentGrid).GetGridIds());
				}
				else if (element is Grid)
				{
					list.Add(element.Id);
				}
			}
			return list;
		}

		private int DealGridByTagStatus(List<ElementId> lstGrid)
		{
			if (this.m_HideRange == 1)
			{
				this.ModifyXdataByCurrentGridStatusSingleView(lstGrid);
			}
			else
			{
				this.ModifyXdataByCurrentGridStatusMutiView(lstGrid);
			}
			return 0;
		}

		private bool ModifyXdataByCurrentGridStatusMutiView(List<ElementId> lstGrid)
		{
			Autodesk.Revit.DB.View view = this.m_Doc.ActiveView;
            Predicate<GridStatusEachView> S9__0 = null;
			foreach (ElementId elementId in lstGrid)
			{
				List<GridStatusEachView> list = this.m_DicGridStatus[elementId];
				List<GridStatusEachView> list2 = list;
				Predicate<GridStatusEachView> match;
				if ((match = S9__0) == null)
				{
					match = (S9__0 = ((GridStatusEachView e) => e.ViewID == view.Id));
				}
				int num = list2.FindIndex(match);
				if (num != -1)
				{
					this.m_Doc.GetElement(elementId);
					GridStatusEachView gridStatusEachView = list[num];
					int num2 = this.JudgeCurrentGridStatus(elementId, gridStatusEachView.nHasTag);
					for (int i = 0; i < list.Count; i++)
					{
						GridStatusEachView value = list[i];
						if (num2 == 0)
						{
							value.nStatus = this.m_HideComment;
						}
						else
						{
							value.nStatus = 0;
						}
						list[i] = value;
					}
					this.m_DicGridStatus[elementId] = list;
				}
			}
			return true;
		}

		private bool ModifyXdataByCurrentGridStatusSingleView(List<ElementId> lstGrid)
		{
			Autodesk.Revit.DB.View view = this.m_Doc.ActiveView;
			Predicate<GridStatusEachView> S9__0 = null;
			foreach (ElementId elementId in lstGrid)
			{
				List<GridStatusEachView> list = this.m_DicGridStatus[elementId];
				List<GridStatusEachView> list2 = list;
				Predicate<GridStatusEachView> match;
				if ((match = S9__0) == null)
				{
					match = (S9__0 = ((GridStatusEachView e) => e.ViewID == view.Id));
				}
				int num = list2.FindIndex(match);
				if (num != -1)
				{
					this.m_Doc.GetElement(elementId);
					GridStatusEachView gridStatusEachView = list[num];
					if (this.JudgeCurrentGridStatus(elementId, gridStatusEachView.nHasTag) == 0)
					{
						gridStatusEachView.nStatus = this.m_HideComment;
					}
					else
					{
						gridStatusEachView.nStatus = 0;
					}
					this.m_DicGridStatus[elementId][num] = gridStatusEachView;
				}
			}
			return true;
		}

		private int JudgeCurrentGridStatus(ElementId GridId, int nHasFlg)
		{
			Grid grid = this.m_Doc.GetElement(GridId) as Grid;
			if ((this.m_Doc.GetElement(grid.GetTypeId()) as GridType).Name.Contains("_NoTag"))
			{
				return 1;
			}
			if (nHasFlg == -1)
			{
				return 0;
			}
			if (nHasFlg == 0 || nHasFlg == 2)
			{
				if (!grid.IsBubbleVisibleInView((DatumEnds)0, this.m_Doc.ActiveView))
				{
					return 2;
				}
				return 0;
			}
			else
			{
				if (nHasFlg != 1 && nHasFlg != 2)
				{
					return 0;
				}
				if (!grid.IsBubbleVisibleInView((DatumEnds)1, this.m_Doc.ActiveView))
				{
					return 2;
				}
				return 0;
			}
		}

		public static bool ModifyGridStatusByXdata(List<ElementId> lstSelGridId, Autodesk.Revit.DB.Document Doc, Dictionary<ElementId, List<GridStatusEachView>> DicGridStatus)
		{
			Transaction transaction = new Transaction(Doc, "轴号隐现");
			bool result;
			try
			{
				transaction.Start();
				Predicate<GridStatusEachView> S9__0 = null;
				foreach (ElementId elementId in lstSelGridId)
				{
					if (DicGridStatus.ContainsKey(elementId))
					{
						List<GridStatusEachView> list = DicGridStatus[elementId];
						List<GridStatusEachView> list2 = list;
						Predicate<GridStatusEachView> match;
						if ((match = S9__0) == null)
						{
							match = (S9__0 = ((GridStatusEachView e) => e.ViewID == Doc.ActiveView.Id));
						}
						int num = list2.FindIndex(match);
						if (num != -1)
						{
							GridStatusEachView gridStatusEachView = list[num];
							int nHasTag = gridStatusEachView.nHasTag;
							switch (gridStatusEachView.nStatus)
							{
							case 0:
								HideOrShowKit.ShowBubbles(nHasTag, Doc, elementId);
								break;
							case 1:
								HideOrShowKit.HideCharact(nHasTag, Doc, elementId);
								break;
							case 2:
								HideOrShowKit.HideCharactAndCircle(nHasTag, Doc, elementId);
								break;
							}
						}
					}
				}
				transaction.Commit();
				result = true;
			}
			catch (Exception)
			{
				if (transaction.IsValidObject && transaction.HasStarted())
				{
					transaction.RollBack();
				}
				result = false;
			}
			return result;
		}

		private Document m_Doc;

		private List<ElementId> m_lstViewId;

		private int m_HideRange = 1;

		private int m_HideComment = 1;

		private int m_SelMode = 1;

		private Dictionary<ElementId, List<GridStatusEachView>> m_DicGridStatus;
	}
}
